package abc.Tool;

import abc.Tool.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

//2.	基于注解的方式
//在 Servlet 的类上上方注明 @WebServlet(urlPatterns = "/xyz") 即可实现对Servlet 的配置。该配置由 servlet-api.jar 提供。
//注意：在一个Java网站中，url-pattern 配置必须是全局唯一。否则会在 tomcat 启动的时候出现异常。

@WebServlet(urlPatterns = "/hsLogin") //
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("u"); //使用 Request 的 getParameter 方法获取查询字符串中键为 u 的值
        String password = req.getParameter("p"); // 使用 Request 的 getParameter 方法获取查询字符串中键为 p 的值
        String code = req.getParameter("c"); // 使用 Request 的 getParameter 方法获取查询字符串中键为 c 的值

        HttpSession session = req.getSession();
        Object ccc = session.getAttribute("CCC");
        boolean isTrue = code != null && code.equals(ccc);
        if(!isTrue){
            //输入的验证码和存储在session中的验证码的值不一致
            PrintWriter writer = resp.getWriter();
            writer.write("code is error");
            writer.flush();
            writer.close();
            return;
        }


        String sql = "SELECT id FROM `user` WHERE username = ? AND `password` = MD5(?)";
        Integer id = JdbcUtils.select(sql,Integer.class,username,password);
        if(id !=null &&id>0){
            //当凭借账号和密码成功查询到id 的时候，将其存储到 session中
            session.setAttribute("UID",id);
            resp.sendRedirect("/abc/index.jsp");
        } else {
            //重定向到登录页
            resp.sendRedirect("abc/login.jsp");
        }
//        String message = id  != null&&id>0?"登录成功":"登录失败";
        // 设置内容类型，使其支持中文显示
//        resp.setContentType("text/html;charset=utf-8");
//
//        System.out.println("username:"+username);
//        System.out.println("password:"+password);
//
//        PrintWriter writer = resp.getWriter();
//
//        writer.write(message);
//        writer.flush();
//        writer.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("u");
        String password = req.getParameter("p");
        String code = req.getParameter("c");


        HttpSession session = req.getSession();
        Object ccc = session.getAttribute("ccc");
        boolean isTrue = code != null && code.equals(ccc);
        if(!isTrue){
            //输入的验证码和存储在session中的验证码的值不一致
            PrintWriter writer = resp.getWriter();
            writer.write("code is error");
            writer.flush();
            writer.close();
            return;
        }

        String sql = "SELECT id FROM `user` WHERE username = ? AND `password`= MD5(?)";
        Long id = JdbcUtils.select(sql,long.class,username,password);
        String message = id  != null&&id>0?"登录成功":"登录失败";
        resp.setContentType("text/html;charset=utf-8");



        System.out.println("username::"+username);
        System.out.println("password:"+password);



        PrintWriter writer = resp.getWriter();

        writer.write(message);
        writer.flush();
        writer.close();
    }
}

